// map.js
import qqmap from '../../../utils/qqmap'
Page({
  data: {
    latitude: 40.060539,
    longitude: 116.343847,
    repairInfo: null,
    address: '广州市天河区吉山幼儿园'
  },
  onLoad({ id }) {
    this.getrepairData(id)
    this.geocoder(this.data.address)
  },
  // 获取保修单详情
  async getrepairData(id) {
    const res = await wx.http({
      url: `/repair/${id}`
    })
    this.setData({
      repairInfo: res.data
    })
  },
  // 根据地理位置获取经纬度
  geocoder(address) {
    qqmap.geocoder({
      address,
      success: (res) => {
        this.setData({
          latitude: res.result.location.lat,
          longitude: res.result.location.lng,
        })
      }
    })
  },
  // 修改信息
  onModification() {
    wx.navigateTo({
      url: '/repair_pkg/pages/form/index?id=' + this.data.repairInfo.id,
    })
  },
  //取消报修
  onDelete() {

    wx.showModal({
      title: '提示',
      content: '点击确认取消报修',
      success: async ({ confirm }) => {
        if (confirm) {
          // 发送请求删除
          const res = await wx.http({
            url: `/cancel/repaire/${this.data.repairInfo.id}`,
            method: 'put'
          })
          if (res.code !== 10000) {
            return wx.utils.toast(res.message)
          }
          // // 弹窗提示
          wx.utils.toast('取消报修成功')
          // 跳转报修列表
          setTimeout(() => {
            wx.redirectTo({ url: '/repair_pkg/pages/list/index' })
          }, 1000);
        }
      }
    })

  }
})
